package com.wzq.animal.back.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.animal.back.pojo.Apply;
import com.wzq.animal.back.pojo.User;
import com.wzq.animal.back.service.ApplyService;
import com.wzq.animal.back.service.SendMailService;
import com.wzq.animal.back.service.UserService;
import com.wzq.animal.back.service.impl.SendMailServiceImpl;
import com.wzq.animal.common.Result;
import com.wzq.animal.common.exception.GlobalException;
import com.wzq.animal.common.exception.StatusCode;
import com.wzq.animal.utils.CipherUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    @Autowired
    private UserService userService;

    @Autowired
    private SendMailService sendMailService;

    /**
     * 全部申请
     * @param currentPage
     * @param pageSize
     * @param usernameKeywords
     * @return
     */
    @GetMapping("/getPages/{currentPage}/{pageSize}")
    public Result getPages(@PathVariable("currentPage") int currentPage,
                           @PathVariable("pageSize") int pageSize,
                           @RequestParam("usernameKeywords") String usernameKeywords){
        Page<Apply> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(usernameKeywords)) {
            queryWrapper.like(Apply::getUsername, usernameKeywords);
        }
        applyService.page(page, queryWrapper);
        return Result.success(page);
    }

    /**
     * 已通过申请
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getSucceed/{currentPage}/{pageSize}")
    public Result getSucceed(@PathVariable("currentPage") int currentPage,
                           @PathVariable("pageSize") int pageSize){
        Page<Apply> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getIsSuccess,1);
        applyService.page(page, queryWrapper);
        return Result.success(page);
    }

    /**
     * 未通过申请
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getApplying/{currentPage}/{pageSize}")
    public Result getApplying(@PathVariable("currentPage") int currentPage,
                             @PathVariable("pageSize") int pageSize){
        Page<Apply> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getIsSuccess,0);
        applyService.page(page, queryWrapper);
        return Result.success(page);
    }

    /**
     * 驳回申请
     * @param apply
     * @return
     */
    @PostMapping("/rejectApply")
    public Result rejectApply(@RequestBody Apply apply){
        apply.setIsSuccess(-1);
        applyService.updateById(apply);
        sendMailService.sendMail(apply.getUsername(),apply.getEmail(),SendMailServiceImpl.contentFailed);
        return Result.success();
    }

    /**
     * 驳回的申请
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getApplyField/{currentPage}/{pageSize}")
    public Result getApplyField(@PathVariable("currentPage") int currentPage,
                              @PathVariable("pageSize") int pageSize){
        Page<Apply> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getIsSuccess,-1);
        applyService.page(page, queryWrapper);
        return Result.success(page);
    }

    @PostMapping("/addApply")
    public Result addApply(@RequestBody Apply apply) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,apply.getUsername());
        List<User> users = userService.list(queryWrapper);
        if(users.size() > 0){
            throw new GlobalException(StatusCode.REGISTER_USERNAME);
        }
        //对密码进行加密
        String encryptApply = CipherUtils.encrypt(apply.getPassword());
        apply.setPassword(encryptApply);
        //存入数据库
        applyService.save(apply);
        String username = apply.getUsername();
        String to = apply.getEmail();
        sendMailService.sendMail(username,to, SendMailServiceImpl.contentApplying);
        return Result.success();
    }

    @DeleteMapping("/delApply/{id}")
    public Result delApply(@PathVariable("id") int id) {
        applyService.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/delApplyBatch")
    public Result delBatch(@RequestBody List<Integer> ids) {
        applyService.removeByIds(ids);
        return Result.success();
    }
}
